//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef _CON_TAB_YG_H__
#define _CON_TAB_YG_H__

#include "def.h"
#include "logctrl.h"
#include "searchimpl.h"
#include "IP2Country.h"
#include "ipresolver.h"

class CConnectionsTab: public wxPanel, public IGUIBind, public CSearchImpl<CConnectionsTab>, public IGUISupport, public ILoggable, public ITabedPageUI, public CIPResolverObserver
{
public:
								CConnectionsTab( class wxWindow* pParent );
	virtual						~CConnectionsTab(void);

	//IGUIBind
	virtual void				OnResponse(const COMMANDRESPONSE& resp);
	virtual void 				SetMaxVal(unsigned int nVal) {}
	virtual void 				SetVal(unsigned int nVal) {}
	virtual void 				SetWarningLevel(unsigned char btVal) {}
	virtual void 				SetAlarmLevel(unsigned char btVal) {}
	virtual void 				SetVal(const wxString& str);
	virtual void				Reset();

	//IGUISupport
	virtual bool				CanSave()	{ return  !m_entries.empty(); }
	virtual wxString			Save();


	//ISearchProvider
	virtual void 				GetSearchFields(SEARCHFIELD*& sf, size_t& sz);
	//virtual int 				Search(const wxString& val, unsigned int iFieldID, size_t posStart);
	virtual cSearchOwnerID		GetID()				{ return cSearchOwnerIDConntrac; }
	virtual void				OnFilterChanged();

	void						OnContextMenu(long id, long col) {}

	bool						OnDropText(wxCoord x, wxCoord y, const wxString& data) { return false; }
	void						OnDragDropFinished( wxWindowID id, wxDragResult result) {}

	long						GetItemCount(wxWindowID id=wxID_ANY)
	{
		return m_entries.size();
	}

	wxString					GetItemText(long item, long column, wxWindowID id=wxID_ANY);

	int							GetItemColumnImage(long item, long column, wxWindowID id=wxID_ANY);
	
	virtual int					GetItemImage(long item, wxWindowID id=wxID_ANY)
	{
		return -1;
	}

	static bool					m_bSortAsc;

	virtual wxString			GetLog();

	//ITabedPageUI
	virtual cTabPageID			GetTabID()					{ return cTabIDConnections; }
	virtual bool				IsTimeSelectorSupported()	{ return true;			}
	virtual time_t				GetTimeInterval()			{ return m_tInt;		}
	virtual void				SetTimeInterval(size_t tInt);

	//CIPResolverObserver
	void						OnResolve(unsigned long lIP, const wxString& strDNSName, void* pCookie);
	


private:
	wxString					GetCountry( const wxString& strIP );
	wxString					GetIPNAME(const wxString& str, wxString& strRes, int iID=0);

protected:
	void						DoSort();
	void						DoFilter();
	void						ProcessData();

	void						UpdateGraph();

	wxString					FieldToString( int iFieldID, CONNTRACENTRY& entry, int iID);

	void						OnActivate(wxActivateEvent&);
	void						OnColumn(wxListEvent& ev);

	void						OnCLick(wxHtmlLinkEvent& event);
	void						ShowAnalyze();
	size_t						GetProtoCountAndTTL(const wxString& strProto,const wxString& strState, size_t& stTTL);
	wxString					BuildTableLine(const wxString& strProblem, const wxString& strSolution, int& nOrder);
	void						SaveAndExecuteNewTimeOuts(const wxString& strField, const wxString& strVal );
	void						FillTimeOutTable();

	void						OnInfoUI(wxUpdateUIEvent& event);
	void						OnInfo(wxCommandEvent& event);
	void						OnResolveCheckBox(wxCommandEvent& event);
	

	DECLARE_EVENT_TABLE()


private:
	

	class wxListCtrl*			m_pList;
	class wxHtmlWindow*			m_pAnylizeWindow;

	typedef std::vector<CONNTRACENTRY>	CONVEC;
	CONVEC						m_entries;
	CONVEC						m_entriesBeforeFilter;

	/*
	STATMAP						m_statProto;
	class CStatCtrl*			m_pStatProtoCtrl;

	STATMAP						m_statSrc;
	class CStatCtrl*			m_pStatSrcCtrl;

	STATMAP						m_statSrcPort;
	class CStatCtrl*			m_pStatSrcPortCtrl;

	STATMAP						m_statDst;
	class CStatCtrl*			m_pStatDstCtrl;


	STATMAP						m_statDstPort;
	class CStatCtrl*			m_pStatDstPortCtrl;

	STATMAP						m_statStatus;
	*/

	unsigned int				m_nSortCol;

	class CSearchPanel*			m_pSearch;
	long						m_lSearchResLine;

	class CLevelGraph*			m_pConGraph;

	class wxButton*				m_pInfoQuery;

	class wxCheckBox*			m_pResolveNames;

	CIP2Country					m_ip2Country;

	size_t						m_lTotal;
	size_t						m_lTCP;
	size_t						m_lUDP;
	size_t						m_lICMP;
	size_t						m_lOther;

	time_t						m_tInt;

	bool						m_bResolve;

	CIPResolver					m_resolver;
};

#endif //_CON_TAB_YG_H__
